<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ElMessage , ElMessageBox} from 'element-plus'
import { Download , Upload} from '@element-plus/icons-vue'

const order = ref([{
    "orderId": '',
    "userId": '',
    "userName": '',
    "serId": '',
    "style": '',
    "status": '',
    "emId": '',
    "emName": '',
    "info": '',
    "createTime": ''
}])

const orderModel = ref({
    orderId: '',
    userId: '',
    userName: '',
    serId: '',
    style: '',
    status: '',
    emId: '',
    emName: '',
    info: '',
    createTime: ''
})

const clearData = ()=>{
    orderModel.value = {
        orderId: '',
        userId: '',
        userName: '',
        serId: '',
        style: '',
        status: '',
        emId: '',
        emName: '',
        info: '',
        createTime: ''
    }
}

const orderSearchModel = ref({
    style: '',
    status: '',
    userId: ''
})

//校验
const rules = ref({
    userId:[{required:true, message:'客户编号不能为空', trigger:'blur'}],
    serId:[{required:true, message:'业务编号不能为空', trigger:'blur'}],
    emId:[{required:true, message:'员工编号不能为空', trigger:'blur'}]
})

// 业务类型下拉选项
const styleOptions = ref([
  { label: '销售管理', value: '销售管理' },
  { label: '售后服务', value: '售后服务' },
  { label: '市场营销', value: '市场营销' },
  { label: '客服服务', value: '客服服务' }
]);

//业务状态下拉选项
const statusOptions = ref([
  { label: '处理中', value: '处理中' },
  { label: '已完成', value: '已完成' }
]);

const title = ref('');
const dialogVisible = ref(false);

import { workOrderListService , workOrderAddService , workOrderDeleteService , workOrderUpdateService , workOrderSearchService} from '@/api/work'
//调用查询接口
const workOrderList = async()=>{
    let result = await workOrderListService();
    order.value = result.data;
}
workOrderList();

//调用创建工单接口
const workOrderAdd = async()=>{
    let result = await workOrderAddService(orderModel.value);
    if(result.code === 0){
        ElMessage.success('创建成功');
    }
    else{
        ElMessage.error('创建失败');
    }
    workOrderList();
    dialogVisible.value = false;
}

//调用退单接口
const workOrderDelete = async(row)=>{
    ElMessageBox.confirm(
        '你确定要退单吗?',
        '温馨提示',
        {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        }
    )
        .then(async () => {
            let result = await workOrderDeleteService(row.orderId);
            ElMessage({
                type: 'success',
                message: '退单成功',
            })
            workOrderList();
            })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '用户取消退单',
            })
        })
}

//回单弹窗
const showUpdate =(row)=>{
    dialogVisible.value = true;
    title.value = '回单';
    orderModel.value.orderId = row.orderId;
    orderModel.value.info = row.info;
}

//调用回单接口
const workOrderUpdate = async()=>{
    let result = await workOrderUpdateService(orderModel.value);
    if(result.code === 0){
        ElMessage.success('回单成功');
    }
    else{
        ElMessage.error('回单失败');
    }
    await workOrderList();
    dialogVisible.value = false;
}

//调用条件查询接口
const search = async()=>{
    const searchParams = {
        style: orderSearchModel.value.style,
        status: orderSearchModel.value.status,
        userId : orderSearchModel.value.userId
    }
    try{
        let result = await workOrderSearchService(searchParams);
        order.value = result.data || [];
        if(order.value.length === 0){
            ElMessage.info('未找到匹配的数据');
        }
    }catch(error){
        ElMessage.error('搜索失败：' + error.message);
    }
}

//重置
const restForm = ()=>{
    orderSearchModel.value.style = '';
    orderSearchModel.value.status = '';
    orderSearchModel.value.userId = '';
    workOrderList();
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>工单管理</span>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible=true; title='创建工单'; clearData()">创建工单</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline :model="orderSearchModel">
            <el-form-item label="业务类型：" prop="style">
                <el-select v-model="orderSearchModel.style" placeholder="请选择业务类型" clearable style="width: 200px">
                <el-option v-for="(item, index) in styleOptions" :key="index" :label="item.label" :value="item.value"></el-option>
                </el-select>  
            </el-form-item>
            <el-form-item label="状态：" prop="status">
                <el-select v-model="orderSearchModel.status" placeholder="请选择业务状态" clearable style="width: 200px">
                <el-option v-for="(item, index) in statusOptions" :key="index" :label="item.label" :value="item.value"></el-option>
                </el-select>  
            </el-form-item>
            <el-form-item label="客户编号：" prop="userId">
                <el-input v-model="orderSearchModel.userId" placeholder="请输入客户编号"></el-input>
            </el-form-item>
            
            <el-form-item>
                <el-button type="primary" @click="search">搜索</el-button>
                <el-button @click="restForm">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="order" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"> </el-table-column>
            <el-table-column label="业务类型" prop="style"></el-table-column>
            <el-table-column label="客户名" prop="userName"></el-table-column>
            <el-table-column label="处理人" prop="emName"></el-table-column>
            <el-table-column label="回单反馈" prop="info"></el-table-column>
            <el-table-column label="创建时间" prop="createTime"></el-table-column>
            <el-table-column label="状态" prop="status"></el-table-column>
            <el-table-column label="操作" width="180">
                <template #default ="{row}">
                    <div style="display: flex; gap: 4px;"> 
                        <el-button type="success" @click="showUpdate(row)" round>回单</el-button>
                        <el-button type="danger" @click="workOrderDelete(row)" round>退单</el-button>
                    </div>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 添加分类弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%">
            <el-form :model="orderModel" :rules="rules" label-width="100px" style="padding-right: 30px">
                <el-form-item  v-if="title==='创建工单'" label="业务编号" prop="serId">
                    <el-input v-model="orderModel.serId" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item v-if="title==='创建工单'" label="客户编号" prop="userId">
                    <el-input v-model="orderModel.userId" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item v-if="title==='创建工单'" label="处理人编号" prop="emId">
                    <el-input v-model="orderModel.emId" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item v-if="title==='回单'" label="回单反馈：" prop="info">
                    <el-input v-model="orderModel.info" minlength="1" maxlength="100"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
             <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="title==='创建工单'? workOrderAdd(): workOrderUpdate()"> 确认 </el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>